package com.android.jay.lib_commin_ui;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;

import android.util.AttributeSet;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

import androidx.core.content.ContextCompat;


/**
 * Created by wzp on 2020/1/8 0008.
 * <p>
 * 描述  ：波纹动画
 */
public class SpreadView extends View {

    private Paint centerPaint; //绘制中心paint
    private int radius = 100;  //中心园的半径
    private Paint spareadPaint; //绘制扩散圆的paint
    private float centerX; //圆心X
    private float centerY; //圆心Y
    private int distance = 5;//每次圆递增的间距
    private int maxRadius = 80; //最大圆半径
    private int delayMilliseconds = 33; //扩散延迟间隔，越大扩散越慢
    private List<Integer> sparedRadius = new ArrayList<>(); //扩散圆层级数
    private List<Integer> alphas = new ArrayList<>(); //对应每层圆的透明度
    private int sparedNum = 8 ;


    public SpreadView(Context context) {
        this(context, null, 0);
    }


    public SpreadView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SpreadView(Context context, AttributeSet attributes, int defStyleAttr) {
        super(context, attributes, defStyleAttr);
        TypedArray a = context.obtainStyledAttributes(attributes,
                R.styleable.SpreadView, defStyleAttr, 0);
        radius = a.getInt(R.styleable.SpreadView_spread_radius, radius);
        maxRadius = a.getInt(R.styleable.SpreadView_spread_max_radius, maxRadius);
        int centerColor = a.getColor(R.styleable.SpreadView_spread_center_color,
                ContextCompat.getColor(context, android.R.color.holo_red_dark));
        int spreadColor = a.getColor(R.styleable.SpreadView_spread_center_color,
                ContextCompat.getColor(context, R.color.color_F71816));
        distance = a.getInt(R.styleable.SpreadView_spread_distance, distance);
        sparedNum = a.getInt(R.styleable.SpreadView_spread_num,sparedNum);
        a.recycle();


        //创建画笔
        centerPaint = new Paint();
        //设置颜色
        centerPaint.setColor(centerColor);
        //打开抗锯齿
        centerPaint.setAntiAlias(true);

        //最开始不透明且扩散度为零 只有一个圆
        alphas.add(255);
        sparedRadius.add(0);

        spareadPaint = new Paint();
        spareadPaint.setAntiAlias(true);
        spareadPaint.setStyle(Paint.Style.STROKE);
        spareadPaint.setAlpha(255);
        spareadPaint.setColor(spreadColor);


    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        centerX = w / 2;
        centerY = h / 2;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //绘制当前所有的圆
        for (int i = 0; i < sparedRadius.size(); ++i) {
            //取出当前圆的透明度给画笔设置
            int alpha = alphas.get(i);
            spareadPaint.setAlpha(alpha);
            //取出当前圆的半径
            int width = sparedRadius.get(i);
            //绘制一个spread圆
            canvas.drawCircle(centerX, centerY, width + radius, spareadPaint);
            // 每次画完后 对当前的圆进行更新 更新圆的透明度
            if (alpha > 0 && width < 300) {
                alpha = alpha - distance > 0 ? alpha - distance : 1;
                alphas.add(alpha);
                sparedRadius.set(i, width + distance);
            }

        }
        //当所有圆画完 最后的半径大于了最大半径 则重置
        if (sparedRadius.get(sparedRadius.size() - 1) > maxRadius) {
            alphas.add(255);
            sparedRadius.add(0);
        }
        //移除多余的圆
        if (sparedRadius.size() >= sparedNum) {
            alphas.remove(0);
            sparedRadius.remove(0);
        }
        canvas.drawCircle(centerX,centerY,radius,centerPaint);
        //设置刷新间隔
        postInvalidateDelayed(delayMilliseconds);
    }
}
